package com.example.snakegame.utils

import android.content.Context
import android.media.AudioManager
import android.media.SoundPool
import android.os.Build
import com.example.snakegame.R

class SoundManager(private val context: Context) {
    private val soundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        SoundPool.Builder().setMaxStreams(5).build()
    } else {
        SoundPool(5, AudioManager.STREAM_MUSIC, 0)
    }
    
    private val soundMap = mutableMapOf<SoundType, Int>()
    
    init {
        soundMap[SoundType.EAT] = soundPool.load(context, R.raw.eat, 1)
        soundMap[SoundType.GAME_OVER] = soundPool.load(context, R.raw.game_over, 1)
        soundMap[SoundType.MOVE] = soundPool.load(context, R.raw.move, 1)
    }
    
    fun playSound(type: SoundType) {
        try {
            soundMap[type]?.let { soundId ->
                soundPool.play(soundId, 1f, 1f, 1, 0, 1f)
            }
        } catch (e: Exception) {
            // 忽略音效播放错误
        }
    }
    
    fun release() {
        try {
            soundPool.release()
        } catch (e: Exception) {
            // 忽略释放错误
        }
    }
    
    enum class SoundType {
        EAT, GAME_OVER, MOVE
    }
} 